package com.java110.mall.user.cmd.userAddress;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.user.UserAddressDto;
import com.java110.intf.user.IUserAddressInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 商铺获取用户地址
 */
@Java110Cmd(serviceCode = "userAddress.queryShopUserAddress")
public class QueryShopUserAddressCmd extends Cmd {

    @Autowired
    private IUserAddressInnerServiceSMO userAddressInnerServiceSMOImpl;
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);
        super.validateShop(context);
        Assert.hasKeyAndValue(reqJson,"tel","未包含手机号");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        UserAddressDto userAddressDto = BeanConvertUtil.covertBean(reqJson, UserAddressDto.class);
        int count = userAddressInnerServiceSMOImpl.queryUserAddresssCount(userAddressDto);

        List<UserAddressDto> userAddressDtos = null;
        if (count > 0) {
            userAddressDtos = userAddressInnerServiceSMOImpl.queryUserAddresss(userAddressDto);
        } else {
            userAddressDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) userAddressDto.getRow()), count, userAddressDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
    }
}
